package com.xcity.game.item.effect;

import java.text.MessageFormat;
import java.util.Date;

import com.xcity.Server;
import com.xcity.db.entity.Player;
import com.xcity.game.common.Cause;
import com.xcity.game.common.MessageConst;
import com.xcity.game.event.GameEvents;
import com.xcity.game.item.GameItem;
import com.xcity.game.item.ItemEffect;
import com.xcity.game.item.ItemException;
import com.xcity.game.item.listener.BagListener;
import com.xcity.game.mail.MailService;
import com.xcity.game.player.PlayerUtils;
import com.xcity.pomelo.push.NoticePush;
import com.xcity.pomelo.push.msg.TipPush;

import naga.x.App;

/**
 * 增加魅力值
 * @author YangLi
 *
 */
public class AddCharmEffect implements ItemEffect {

	/** 魅力值改变类型（点赞、送玫瑰） */
	public static enum CharmDeltaType {
		UNKNOWN(0), TAP_LIKE(0), ROSE(10013), ROSE99(10023);
		public final int itemId;
		CharmDeltaType(int itemId) {
			this.itemId = itemId;
		}
		public static CharmDeltaType of(int itemId) {
			CharmDeltaType[] vals = values();
			for (CharmDeltaType v : vals) {
				if (v.itemId == itemId) {
					return v;
				}
			}
			return null;
		}
	}
	
	@Override
	public boolean effect(Player player, GameItem item, int count, BagListener listener) throws ItemException {
		int val = item.getTemplate().getParameter(0) * count;
		Player source = item.getBag().getOwner();
		String cause = Cause.USE_ITEM;
		if (source.getId() != player.getId()) {
			cause += source.getId();
		}
		CharmDeltaType type = CharmDeltaType.of(item.getTemplate().getId());
		if (PlayerUtils.addCharm(player, val, cause, type)) {
			source.send(new TipPush(MessageConst.PRESENT_ROSE_SUCC));
			if (type == CharmDeltaType.ROSE) {
				String notice = MessageFormat.format(MessageConst.PRESENT_ROSE_TO_SELF, source.getName(), item.getName());
				player.send(new NoticePush(NoticePush.NOTICE_MARQUEE, notice));
			} else {
				String notice = MessageFormat.format(MessageConst.PRESENT_ROSE, source.getName(), item.getName(), player.getName());
				Server.broadcast(new NoticePush(NoticePush.NOTICE_ROSE99, notice));
			}
			App.getApp().getServiceManager().get(MailService.class).sendSystemMail(
					player.getId(),
					MessageConst.RECEIVE_ROSE_MAIL_TITLE,
					MessageFormat.format(MessageConst.RECEIVE_ROSE_MAIL_CONTENT, source.getName(), source.getId(), new Date(), item.getName(), count));
			App.getApp().getEventManager().addEvent(GameEvents.EVENT_PRESENT_ROSE, source, player, item.getTemplate().getId(), count);
			return true;
		}
		throw new ItemException(MessageConst.CHARM_FULL);
	}

}
